package com.saint.easemobtest;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.saint.easemobtest.runtimepermissions.PermissionsManager;
import com.saint.easemobtest.runtimepermissions.PermissionsResultAction;

import org.xutils.view.annotation.ViewInject;

public class LoginActivity extends AppCompatActivity {
    private Context mContext;

//    @ViewInject(R.id.user_name_et)
    private EditText userName_et;
//    @ViewInject(R.id.pwd_et)
    private EditText pwd_et;
//    @ViewInject(R.id.tips_tv)
    private TextView tips_tv;

    private String userName,pwd;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1://登录成功
                    Toast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent(mContext, MainActivity.class);
                    startActivity(intent);

                    break;

                case 2://登录失败
                    Toast.makeText(mContext, "登录失败", Toast.LENGTH_SHORT).show();
                    tips_tv.setText("登录失败： code: " + msg.arg1 + "  error: " + msg.obj.toString());


                    break;
                case 3://注册失败
                    HyphenateException e = (HyphenateException) msg.obj;
                    tips_tv.setText("注册失败： code: " + e.getErrorCode() + "  Msg: " + e.getMessage());
                    break;
                case 4://注册成功
                    tips_tv.setText("注册成功： " + msg.obj.toString());
                    break;

                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mContext = this;

        userName_et = (EditText) findViewById(R.id.user_name_et);
        pwd_et = (EditText) findViewById(R.id.pwd_et);
        tips_tv = (TextView) findViewById(R.id.tips_tv);

        getPermissions();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_btn:
                registerUser();
                break;
            case R.id.login_btn:
                login();
                break;

            default:
                break;
        }
    }

    private void registerUser(){
        tips_tv.setText("");
        userName = userName_et.getText().toString().trim();
        pwd = pwd_et.getText().toString().trim();
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(pwd)){
            Toast.makeText(mContext, "用户名或密码不可为空", Toast.LENGTH_SHORT).show();
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().createAccount(userName, pwd);
                    Message msg = Message.obtain();
                    msg.what = 4;
                    msg.obj = "账号： " + userName + "  密码： " + pwd ;
                    handler.sendMessage(msg);
                } catch (HyphenateException e) {
                    e.printStackTrace();
                    Message msg = Message.obtain();
                    msg.what = 3;
                    msg.obj = e;
                    handler.sendMessage(msg);
                }
            }
        }).start();
    }

    private void login(){
        tips_tv.setText("");
        userName = userName_et.getText().toString().trim();
        pwd = pwd_et.getText().toString().trim();
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(pwd)){
            Toast.makeText(mContext, "用户名或密码不可为空", Toast.LENGTH_SHORT).show();
            return;
        }
        EMClient.getInstance().login(userName, pwd, new EMCallBack() {
            @Override
            public void onSuccess() {
                handler.sendEmptyMessage(1);
            }

            @Override
            public void onError(int code, String error) {
                Message msg = Message.obtain();
                msg.what = 2;
                msg.arg1 =  code;
                msg.obj = error;
                handler.sendMessage(msg);
            }

            @Override
            public void onProgress(int progress, String status) {

            }
        });
    }

    private void getPermissions() {
        /**
         * 请求所有必要的权限----原理就是获取清单文件中申请的权限
         */
        PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() {
            @Override
            public void onGranted() {
                Toast.makeText(mContext, "All permissions have been granted", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDenied(String permission) {
                Toast.makeText(mContext, "Permission " + permission + " has been denied", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
